package com.example.mybatis.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.example.mybatis.business.IUserBusiness;
import com.example.mybatis.form.UserForm;
import com.example.mybatis.model.PageInfo;
import com.example.mybatis.model.ServiceResponse;
import com.example.mybatis.model.UserTO;
import com.example.mybatis.service.IUserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.List;


@Service
public class UserService implements IUserService {
    @Autowired
    private IUserBusiness userBusiness;

    private  static final Logger logger = LoggerFactory.getLogger(UserService.class);
    @Override
    public ServiceResponse<UserTO> queryUserInfoByAccount(String userAccount) {
        ServiceResponse<UserTO> response = new ServiceResponse<>();
        UserTO userTO = userBusiness.queryUsersByAccount(userAccount);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData(userTO);
        return response;
    }

    @Override
    public ServiceResponse<String> addUserTO(UserTO userTO) throws Exception{
        ServiceResponse<String> response = new ServiceResponse<>();
            userBusiness.addUserInfo(userTO);
            response.setMessage("ok");
            response.setStatus(200);
            response.setData("success");
            return response;
    }

    @Override
    public ServiceResponse<String> updateUserTO(UserTO userTO)throws Exception {
        ServiceResponse<String> response = new ServiceResponse<>();
            userBusiness.updateUserInfo(userTO);
            response.setMessage("ok");
            response.setStatus(200);
            response.setData("success");

        return response;
    }

    @Override
    public ServiceResponse<String> batchAddUserTO(List<UserTO> userTOs) throws Exception{
        ServiceResponse<String> response = new ServiceResponse<>();
        userBusiness.batchInsertUserInfo(userTOs);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData("success");
        return response;
    }

    @Override
    public ServiceResponse<PageInfo> selectAllUser(int pageNum, int pageSize) {
        ServiceResponse<PageInfo> response = new ServiceResponse<>();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        int startIndex = (pageNum-1)*pageSize+1;
        int endIndex = pageNum*pageSize;
        pageInfo.setStartIndex(startIndex);
        int totalRows = userBusiness.queryTotalRows("userinfo");
        if(totalRows<endIndex){
            endIndex = totalRows;
        }
        pageInfo.setEndIndex(endIndex);
        pageInfo.setTotalRows(totalRows);
        List<UserTO> resultList = userBusiness.selectAllUser(startIndex,pageSize-1);
        pageInfo.setDataList(resultList);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData(pageInfo);
        return response;
    }

    @Override
    public ServiceResponse<String> batchUpdateUserInfo(List<UserTO> userItems) throws Exception{
        ServiceResponse<String> response = new ServiceResponse<>();
        userBusiness.batchUpdateUserInfo(userItems);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData("success!");

        return response;
    }

    @Override
    public ServiceResponse<String> deleteUserInfo(String userAccount) throws Exception{
        ServiceResponse<String> response = new ServiceResponse<>();
        userBusiness.deleteUserInfo(userAccount);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData("success!");

        return response;
    }

    @Override
    public ServiceResponse<String> batchDeleteUserInfo(List<String> userAccounts) throws Exception{
        ServiceResponse<String> response = new ServiceResponse<>();
        userBusiness.batchDeleteUserInfo(userAccounts);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData("success!");
        return response;
    }

    @Override
    public ServiceResponse<UserTO> userLogin(UserForm userForm) {
        logger.info("userAccount={}",userForm.getUserAccount());
        ServiceResponse<UserTO> serviceResponse = new ServiceResponse<>();
        UserTO userInfo = userBusiness.queryUsersByAccount(userForm.getUserAccount());
        if(userInfo!=null && userForm.getPassword().equals(userInfo.getPassword())){
            serviceResponse.setStatus(200);
            serviceResponse.setMessage("ok");
            serviceResponse.setData(userInfo);
        }else{
            serviceResponse.setStatus(405);
            serviceResponse.setMessage("login failed");
        }
        return serviceResponse;
    }

    @Override
    public ServiceResponse<List<UserTO>> searchUsers(UserForm userForm) throws Exception{
//        logger.info("userForm={}", JSONArray.toJSONString(userForm));
        ServiceResponse<List<UserTO>> response = new ServiceResponse<>();
        List<UserTO> users = userBusiness.searchUsers(userForm);
        response.setStatus(200);
        response.setMessage("ok");
        response.setData(users);
        return response;
    }
}
